package com.taomu.app.zongchouhuli.moudle.mine.activity;

import android.app.Dialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.http.res.ResMyBuyProductEntity;
import com.taomu.app.zongchouhuli.http.res.ResMyProductEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductRebateEntity;
import com.taomu.app.zongchouhuli.http.res.ResRebateDeatailEntity;
import com.taomu.app.zongchouhuli.moudle.mine.adpter.RebateDetailAdp;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserRebateDetailContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.UserRebateDetailPresenter;
import com.taomu.app.zongchouhuli.moudle.mine.entity.RebateDetailEntity;
import com.taomu.app.zongchouhuli.utils.TimeUtils;
import com.taomu.app.zongchouhuli.utils.dialog.SelectBalanceTypeDialog;
import com.taomu.app.zongchouhuli.utils.dialog.SelectBankDialog;
import com.taomu.app.zongchouhuli.utils.entity.SelectBalanceEntity;
import com.taomu.app.zongchouhuli.utils.entity.SelectBankEntity;

import java.util.ArrayList;
import java.util.List;

public class UserRebateDetailActivity extends BaseActivity<UserRebateDetailContract.Presenter> implements UserRebateDetailContract.View {

    public static final String REBATEPRODUCTID = "rebateproductid";

    public static final String REBATEPRODUCTINFO = "REBATEPRODUCTINFO";

    private ImageView mBackIm;

    private ImageView mIconIm;

    private TextView mProductNameTv;

    //产品价格
    private TextView mPriceTv;

    //产品数量
    private TextView mProductNumTv;

    //购买时间
    private TextView mBuyTimeTv;

    //结算方式
    private TextView mBalacneTypeTv;

    //更换结算方式
    private TextView mBalanceChangedTv;

    //日返利
    private TextView mDayBalanceTv;

    //日收益率
    private TextView mDayIncomeTv;

    //结算合计
    private TextView mBalanceTotalTv;

    private TextView mSellTv;

    private TextView mStatusTv;

    private RecyclerView mBalanceRecy;

    private RebateDetailAdp mAdp;

    private SmartRefreshLayout mRefreshLayout;

    private ResMyBuyProductEntity.DataBean.ListBean currenMyProductInfo;

    private int ship = 0;

    private int take = 10;

    @Override
    protected void onDestroy() {
        mPresenter.detch();
        super.onDestroy();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_rebate_detail;
    }

    @Override
    protected void initView() {
        mRefreshLayout = findViewById(R.id.smart_refresh);
        mStatusTv = findViewById(R.id.rebate_detail_product_status_tv);
        mSellTv = findViewById(R.id.rebate_detail_sell_tv);
        mBackIm = findViewById(R.id.rebate_detail_back_im);
        mIconIm = findViewById(R.id.rebate_detail_product_icon_im);
        mPriceTv = findViewById(R.id.rebate_detail_price_tv);
        mProductNameTv = findViewById(R.id.rebate_detail_product_name_tv);
        mProductNumTv = findViewById(R.id.rebate_detail_product_num_tv);
        mBuyTimeTv = findViewById(R.id.rebate_detail_time_tv);
        mBalacneTypeTv = findViewById(R.id.rebate_detail_balance_type_tv);
        mBalanceChangedTv = findViewById(R.id.rebate_detail_balance_change_tv);
        mDayBalanceTv = findViewById(R.id.rebate_detail_rebate_tv);
        mDayIncomeTv = findViewById(R.id.rebate_detail_income_tv);
        mBalanceTotalTv = findViewById(R.id.rebate_detail_balance_total_tv);
        mAdp = new RebateDetailAdp(this);
        mBalanceRecy = findViewById(R.id.rebate_detail_balance_detail_recy);
        mBalanceRecy.setLayoutManager(new LinearLayoutManager(this));
        mBalanceRecy.setAdapter(mAdp);
        mBalanceRecy.setNestedScrollingEnabled(false);
    }

    String status = "";

    String[] balances = {"日结","周结","月结"};

    String cuurenttype = "";

    String id = "-1";

    @Override
    protected void initData() {
        if(getIntent() != null){
            id = getIntent().getStringExtra(REBATEPRODUCTID);
            currenMyProductInfo = (ResMyBuyProductEntity.DataBean.ListBean) getIntent().getSerializableExtra(REBATEPRODUCTINFO);
            mPresenter.getRebateDetailList(ship,take,id);
        }
        entities = new ArrayList<>();
        if(currenMyProductInfo != null){
            if(!TextUtils.isEmpty(currenMyProductInfo.getProduct_img_uri())) {
                Glide.with(this).load(currenMyProductInfo.getProduct_img_uri()).into(mIconIm);
            }
            mProductNumTv.setText("数量:"+String.valueOf(currenMyProductInfo.getQuantity()));
            if(!TextUtils.isEmpty(currenMyProductInfo.getCreate_date())) {
                mBuyTimeTv.setText("购买时间:"+TimeUtils.timeFomartOnlyDate(Long.valueOf(currenMyProductInfo.getCreate_date())));
            }
            mDayBalanceTv.setText(String.valueOf(currenMyProductInfo.getIncome()));
            mDayIncomeTv.setText(currenMyProductInfo.getPer()+"%");
            if("day_income".equals(currenMyProductInfo.getIncome_type())){
                mBalacneTypeTv.setText("日结");
                cuurenttype = "day_income";
            }else if("week_income".equals(currenMyProductInfo.getIncome_type())){
                mBalacneTypeTv.setText("周结");
                cuurenttype = "week_income";
            }else if("month_income".equals(currenMyProductInfo.getIncome_type())){
                mBalacneTypeTv.setText("月结");
                cuurenttype = "month_income";
            }
            mProductNameTv.setText(currenMyProductInfo.getProduct_name());
            mPriceTv.setText("¥"+currenMyProductInfo.getProduct_price());
            mAdp.setProductName(currenMyProductInfo.getProduct_name());
            status = currenMyProductInfo.getPay_status_id();
            if("product_pay_done".equals(currenMyProductInfo.getPay_status_id())){
                mStatusTv.setText("已购买");
            }else if("product_pay_clear".equals(currenMyProductInfo.getPay_status_id())){
                mStatusTv.setText("已撤资");
            }else if("product_pay_divestment".equals(currenMyProductInfo.getPay_status_id())){
                mStatusTv.setText("申请撤资");
            }else if("product_pay_wait".equals(currenMyProductInfo.getPay_status_id())){
                mStatusTv.setText("待支付");
            }
        }
        //scrollview+recyview or listview 可能会自动滚到最底部
        //  解决方法:
        //  将scrollview包裹的内容设置上以下两个属性
        //  android:focusable="true"
        //  android:focusableInTouchMode="true"
        mPresenter.getRebateDetail(id);
    }

    @Override
    protected void initEvent() {
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mBalanceChangedTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("product_pay_done".equals(status)) {
                    showSelectDialog();
                }else{
                    showToast("当前状态不能更改结算方式");
                }
            }
        });
        mSellTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.sellProduct(id);
            }
        });
        mRefreshLayout.setOnRefreshListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mRefreshLayout.setEnableLoadMore(true);
                ship = 0;
                mPresenter.getRebateDetailList(ship,take,id);
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mPresenter.getRebateDetailList(ship,take,id);
            }
        });
    }

    @Override
    protected UserRebateDetailContract.Presenter createPresenter() {
        return new UserRebateDetailPresenter(this);
    }

    @Override
    public void setRebateDetailList(BaseChangeResponse<ResProductRebateEntity> mData) {
        if(!TextUtils.isEmpty(mData.getData().getTotal_fee())) {
            if("null".equals(mData.getData().getTotal_fee())) {
                mBalanceTotalTv.setText("¥0");
            }else{
                mBalanceTotalTv.setText("¥" + String.valueOf(mData.getData().getTotal_fee()));
            }
        }else{
            mBalanceTotalTv.setText("¥0");
        }
        if(mData.getData().getList() != null){
            if(mRefreshLayout.isRefreshing()){
                mAdp.setData(mData.getData().getList());
            }else if(mRefreshLayout.isLoading()){
                mAdp.addData(mData.getData().getList());
            }else{
                mAdp.setData(mData.getData().getList());
            }
        }
        ship = mAdp.getItemCount();
        if(Integer.valueOf(mData.getCount())>mAdp.getItemCount()){
            mRefreshLayout.setEnableLoadMore(true);
        }else{
            mRefreshLayout.setEnableLoadMore(false);
        }
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }

    @Override
    public void setSellResults() {
        mStatusTv.setText("申请撤资");
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_tip, null);
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(view);
        TextView tv = view.findViewById(R.id.dialog_confirm_tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    public void setChanged(String msg) {
        if(mClickSelectData != null) {

            selectData = mClickSelectData;
            mBalacneTypeTv.setText(selectData.getTitle());
            mDayBalanceTv.setText(selectData.getIncome_evey());
            mDayIncomeTv.setText(selectData.getIncome() + "%");
            if("日结".equals(selectData.getTitle())) {
                cuurenttype = "day_income";
            }else if("周结".equals(selectData.getTitle())){
                cuurenttype = "week_income";
            }else if("月结".equals(selectData.getTitle())){
                cuurenttype = "month_income";
            }
        }
        showToast(msg);
    }

    @Override
    public void setRebateDetail(ResRebateDeatailEntity entity) {
        entities.clear();
        if(entity != null){
            if(entity.getData().getIncome() != null){
                if(entity.getData().getIncome().getDay() != null){
                    if(entity.getData().getIncome().getDay().size()>=2){
                        SelectBalanceEntity entity1 = new SelectBalanceEntity();
                        entity1.setIncome(String.valueOf(entity.getData().getIncome().getDay().get(0)));
                        entity1.setIncome_evey(String.valueOf(entity.getData().getIncome().getDay().get(1)));
                        entity1.setTitle("日结");
                        entities.add(entity1);
                        if(cuurenttype.equals("day_income")){
                            selectData = entity1;
                        }
                    }
                }
                if(entity.getData().getIncome().getWeek() != null){
                    if(entity.getData().getIncome().getWeek().size()>=2){
                        SelectBalanceEntity entity1 = new SelectBalanceEntity();
                        entity1.setIncome(String.valueOf(entity.getData().getIncome().getWeek().get(0)));
                        entity1.setIncome_evey(String.valueOf(entity.getData().getIncome().getWeek().get(1)));
                        entity1.setTitle("周结");
                        entities.add(entity1);
                        if(cuurenttype.equals("week_income")){
                            selectData = entity1;
                        }
                    }
                }
                if(entity.getData().getIncome().getMonth() != null){
                    if(entity.getData().getIncome().getMonth().size()>=2){
                        SelectBalanceEntity entity1 = new SelectBalanceEntity();
                        entity1.setIncome(String.valueOf(entity.getData().getIncome().getMonth().get(0)));
                        entity1.setIncome_evey(String.valueOf(entity.getData().getIncome().getMonth().get(1)));
                        entity1.setTitle("月结");
                        entities.add(entity1);
                        if(cuurenttype.equals("month_income")){
                            selectData = entity1;
                        }
                    }
                }
            }
        }
    }

    private void showDialog(){

    }

    @Override
    public void dismissLoadingDialog() {
        super.dismissLoadingDialog();
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
    }

    private List<SelectBalanceEntity> entities;

    private SelectBalanceTypeDialog bankdialog;

    private SelectBalanceEntity selectData;

    private SelectBalanceEntity mClickSelectData;

    private void showSelectDialog(){
//        if(bankdialog == null) {
            bankdialog = new SelectBalanceTypeDialog();
            bankdialog.setData(entities,selectData);
            bankdialog.setSelectBalanceListener(new SelectBalanceTypeDialog.SelectBalanceListener() {
                @Override
                public void onDismiss(SelectBalanceEntity entity) {
                    String selectType = "";
                    if("日结".equals(entity.getTitle())){
                        selectType = "day_income";
                    }else if("周结".equals(entity.getTitle())){
                        selectType = "week_income";
                    }else if("月结".equals(entity.getTitle())){
                        selectType = "month_income";
                    }
                    if(cuurenttype.equals(selectType)){
                        return;
                    }
                    if("日结".equals(entity.getTitle())) {
                        //cuurenttype = "day_income";
                        mPresenter.changedJiesun(String.valueOf(currenMyProductInfo.getId()), "day_income");
                    }else if("周结".equals(entity.getTitle())){
                        //cuurenttype = "week_income";
                        mPresenter.changedJiesun(String.valueOf(currenMyProductInfo.getId()), "week_income");
                    }else if("月结".equals(entity.getTitle())){
                        //cuurenttype = "month_income";
                        mPresenter.changedJiesun(String.valueOf(currenMyProductInfo.getId()), "month_income");
                    }
                    mClickSelectData = entity;
                }
            });
            bankdialog.show(getSupportFragmentManager(), "select");
//        }else{
//            bankdialog.setData(entities,selectData);
//            bankdialog.show(getSupportFragmentManager(),"select");
//        }
    }


    @Override
    public void setPresenter(UserRebateDetailContract.Presenter presenter) {

    }
}
